package com.buobao.oa.controller;

import com.alibaba.fastjson.JSON;
import com.buobao.oa.dto.OvertimeFormDto;
import com.buobao.oa.entity.OvertimeForm;
import com.buobao.oa.entity.ProcessFlow;
import com.buobao.oa.entity.User;
import com.buobao.oa.service.OvertimeFormService;
import com.buobao.oa.service.ProcessFlowService;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "OvertimeServlet", urlPatterns = "/overtime/*")
public class OvertimeServlet extends HttpServlet {
    private OvertimeFormService overtimeFormService = new OvertimeFormService();
    private ProcessFlowService processFlowService = new ProcessFlowService();
    private Logger logger= LoggerFactory.getLogger(OvertimeServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String uri = req.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/")+1);
        if ("create".equals(methodName)) {
            //创建加班单
            create(req,resp);
        } else if ("list".equals(methodName)) {
            //审批列表
            list(req,resp);
        } else if ("audit".equals(methodName)) {
            //审批
            audit(req,resp);
        } else if ("mylist".equals(methodName)) {
            //我的列表
            mylist(req,resp);
        }
    }

    private void mylist(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("login_user");
        int page = Integer.parseInt(req.getParameter("page"));
        int limit = Integer.parseInt(req.getParameter("limit"));
        Page<OvertimeForm> pager = overtimeFormService.selectSelfByEmployeeId(user.getEmployeeId(),page,limit);
        HashMap<String,Object> result = new HashMap<>();
        result.put("code","0");
        result.put("message","success");
        result.put("data",pager.getResult());
        result.put("count",pager.getTotal());
        String json = JSON.toJSONString(result);
        resp.getWriter().println(json);
    }

    private void audit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String formId = req.getParameter("formId");
        String reason = req.getParameter("reason");
        String result = req.getParameter("result");
        overtimeFormService.audit(Long.parseLong(formId),reason,result);

        HashMap<String,Object> webresult = new HashMap<>();
        webresult.put("code","0");
        webresult.put("message","success");
        String json = JSON.toJSONString(webresult);
        resp.getWriter().println(json);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("login_user");
        List<OvertimeFormDto> overtimeForms = overtimeFormService.selectFormByEmployee(user.getEmployeeId());
        HashMap<String,Object> result = new HashMap<>();
        result.put("code","0");
        result.put("message","success");
        result.put("data",overtimeForms);
        result.put("count",overtimeForms.size());
        String json = JSON.toJSONString(result);
        resp.getWriter().println(json);
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User loginUser = (User) session.getAttribute("login_user");
        String formType = req.getParameter("formType");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String reason = req.getParameter("reason");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH");
        HashMap<String,Object> result = new HashMap<>();

        try {
            OvertimeForm overtimeForm = new OvertimeForm();
            overtimeForm.setEmployeeId(loginUser.getEmployeeId());
            overtimeForm.setStartTime(simpleDateFormat.parse(startTime));
            overtimeForm.setEndTime(simpleDateFormat.parse(endTime));
            overtimeForm.setFormType(Integer.parseInt(formType));
            overtimeForm.setReason(reason);
            overtimeForm.setCreateTime(new Date());
            overtimeFormService.createOvertimeForm(overtimeForm);
            result.put("code",0);
            result.put("message","success");
        } catch (ParseException e) {
            logger.error("加班申请异常",e);
            result.put("code",e.getClass().getSimpleName());
            result.put("message",e.getMessage());
        }
        String json = JSON.toJSONString(result);
        resp.getWriter().println(json);
    }
}
